package com.lenovo.agricultureclient.request;

import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Element;

import android.util.Log;

import com.lenovo.agricultureclient.bean.SensorValue;
import com.lenovo.agricultureclient.util.Util;
/**
 * 获取传感器实时值的请求类
 * @author zongbingwu
 *
 */
public class GetSensorRequest extends AgricultureRequest 
{
	//action name
	private static final String ACTION = "getSensor";
	//存放传感器实时的值
	private SensorValue sensorValue;
	
	public SensorValue getSensorValue() {
		return sensorValue;
	}
	public void setSensorValue(SensorValue sensorValue) {
		this.sensorValue = sensorValue;
	}

	@Override
	protected String getActionName() {
		return ACTION;
	}
	
	//jason协议时的获取body函数，不必携带额外参数
	@Override
	protected String onGetJasonBody() 
	{
		return "{}"; //不能为空字符串
	}
	
	//soap协议时的获取body函数，不必携带额外参数
	@Override
	protected String onGetSoapBody() 
	{
		return Util.getSoapXml("");
	}
	
	//jason协议时解析回应函数
	@Override
	protected void onJasonParese(String responseStr) 
	{
		try 
		{
			JSONObject jsonRespObj = new JSONObject(responseStr);
			if(jsonRespObj!=null && sensorValue!=null)
			{
				if(jsonRespObj.has("result"))
				{
					String result = jsonRespObj.getString("result");
					result = result.toLowerCase();
					//判断请求是否成功
					if(result.equals("ok")){
						isSuccess = true;
					} else {
						isSuccess = false;
					}
				}
				//空气湿度
				sensorValue.setUpdataTime(new Date());
				if(jsonRespObj.has("airHumidity")){
					sensorValue.setAirHumid(jsonRespObj.getInt("airHumidity"));
				}
				//空气温度
				if(jsonRespObj.has("airTemperature")){
					sensorValue.setAirTemper(jsonRespObj.getInt("airTemperature"));
				}
				//PM2.5浓度
				if(jsonRespObj.has("PM25")){
					sensorValue.setPm25(jsonRespObj.getInt("PM25"));
				}
				//土壤温度
				if(jsonRespObj.has("soilTemperature")){
					sensorValue.setSoilTemper(jsonRespObj.getInt("soilTemperature"));
				}
				//CO2浓度
				if(jsonRespObj.has("co2")){
					sensorValue.setCo2(jsonRespObj.getInt("co2"));
				}
				//土壤湿度
				if(jsonRespObj.has("soilHumidity")){
					sensorValue.setSoilHumid(jsonRespObj.getInt("soilHumidity"));
				}
				//灯光强度
				if(jsonRespObj.has("light")){
					sensorValue.setLight(jsonRespObj.getInt("light"));
				}
			}
			else {
				isSuccess = false;
			}
		} 
		catch (JSONException e) 
		{
			isSuccess = false;
			e.printStackTrace();
			Log.e(ACTION,"response string is:"+responseStr);
		}
	}
	
	//soap协议时解析回应函数
	@Override
	protected void onSoapParese(String responseStr) 
	{
		//从完整的soap协议内容中解析出soap body内容
		Element soapBodyEle = Util.getSoapBodyElement(responseStr);
		if(soapBodyEle!=null && sensorValue!=null)
		{
			try 
			{
				String result = Util.getChildElementValueStr(soapBodyEle, "result");
				result = result.toLowerCase();
				//判断请求是否成功
				if(result.equals("ok")){
					isSuccess = true;
				} else {
					isSuccess = false;
				}
				sensorValue.setUpdataTime(new Date());
				//空气湿度
				Element airHumidity = (Element)soapBodyEle.getElementsByTagName("airHumidity").item(0);
				if(airHumidity != null){
					sensorValue.setAirHumid(Integer.parseInt(airHumidity.getFirstChild().getNodeValue()));
				}
				//空气温度
				Element airTemperature = (Element)soapBodyEle.getElementsByTagName("airTemperature").item(0);
				if(airTemperature!=null){
					sensorValue.setAirTemper(Integer.parseInt(airTemperature.getFirstChild().getNodeValue()));
				}
				//PM2.5浓度
				Element pm25 = (Element)soapBodyEle.getElementsByTagName("PM25").item(0);
				if(pm25!=null){
					sensorValue.setPm25(Integer.parseInt(pm25.getFirstChild().getNodeValue()));
				}
				//土壤温度
				Element soilTemperature = (Element)soapBodyEle.getElementsByTagName("soilTemperature").item(0);
				if(soilTemperature!=null){
					sensorValue.setSoilTemper(Integer.parseInt(soilTemperature.getFirstChild().getNodeValue()));
				}
				//CO2浓度
				Element co2 = (Element)soapBodyEle.getElementsByTagName("co2").item(0);
				if(co2!=null){
					sensorValue.setCo2(Integer.parseInt(co2.getFirstChild().getNodeValue()));
				}
				//土壤湿度
				Element soilHumidity = (Element)soapBodyEle.getElementsByTagName("soilHumidity").item(0);
				if(soilHumidity!=null){
					sensorValue.setSoilHumid(Integer.parseInt(soilHumidity.getFirstChild().getNodeValue()));
				}
				//灯光强度
				Element light = (Element)soapBodyEle.getElementsByTagName("light").item(0);
				if(light!=null){
					sensorValue.setLight(Integer.parseInt(light.getFirstChild().getNodeValue()));
				}
				
			} catch (Exception e) {
				e.printStackTrace();
				isSuccess = false;
			}
		} else {
			isSuccess = false;
		}
	}

}
